package cn.wolfcode.luowowo.service.impl;


import cn.wolfcode.luowowo.domain.Strategy;
import cn.wolfcode.luowowo.domain.StrategyCatalog;
import cn.wolfcode.luowowo.query.StrategyCatalogObjcet;
import cn.wolfcode.luowowo.repository.RegionRepository;
import cn.wolfcode.luowowo.repository.StrategyCatalogRepository;
import cn.wolfcode.luowowo.service.IRegionService;
import cn.wolfcode.luowowo.service.IStrategyCatalogService;
import cn.wolfcode.luowowo.service.IStrategyService;
import cn.wolfcode.luowowo.util.DBHelper;
import cn.wolfcode.luowowo.vo.CatalogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StrategyCatalogServiceImpl implements IStrategyCatalogService {
    @Autowired
    private MongoTemplate template;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private StrategyCatalogRepository strategyCatalogRepository;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private RegionRepository regionRepository;
    @Override
    public StrategyCatalog get(String id) {
        return  strategyCatalogRepository.findById(id).orElse(null);
    }

    @Override
    public List<CatalogVO> list() {
        List<CatalogVO> list = new ArrayList<>();
        TypedAggregation<StrategyCatalog> agg = Aggregation.newAggregation(StrategyCatalog.class,
                Aggregation.group("destName").
                        push("name").as("names").
                        push("id").as("ids")
        );
        AggregationResults<Map> result = template.aggregate(agg,Map.class);
        List<Map> datas = result.getMappedResults();
        for (Map data : datas) {
            CatalogVO vo = new CatalogVO();

            vo.setDestName(data.get("_id").toString());

            List<Map<String, Object>> mm = new ArrayList<>();
            List<Object> names = (List<Object>) data.get("names");
            List<Object> ids = (List<Object>) data.get("ids");
            for (int i = 0;i < names.size(); i++) {
                Map<String, Object> vv = new HashMap<>();
                String name = names.get(i).toString();
                String id = ids.get(i).toString();
                vv.put("id", id);
                vv.put("name", name);
                mm.add(vv);
            }
            vo.setMapList(mm);
            list.add(vo);
        }
        System.out.println(list);
        return list;
    }




    @Override
    public Page<StrategyCatalog> query(StrategyCatalogObjcet qo) {
        //创建查询对象
        Query query = new Query();
        //分页查询

        PageRequest request = PageRequest.of(qo.getCurrentPage()-1, qo.getPageSize(), Sort.Direction.ASC, "_id");
        //关键字查询
        if(StringUtils.hasLength(qo.getKeyword())){
            query.addCriteria(Criteria.where("name").regex(".*"+qo.getKeyword()+".*"));
        }
       return DBHelper.query(template,StrategyCatalog.class,query,request);
    }


    @Override
    public void saveOrUpdate(StrategyCatalog strategyCatalog) {
        if(strategyCatalog.getId()==null){
            this.save(strategyCatalog);
        }else{
            this.update(strategyCatalog);
        }
    }

    @Override
    public void delete(String id) {
        strategyCatalogRepository.deleteById(id);
    }

    @Override
    public void save(StrategyCatalog strategyCatalog) {
        strategyCatalog.setId(null);
        strategyCatalogRepository.save(strategyCatalog);
    }

    @Override
    public void update(StrategyCatalog strategyCatalog) {
        strategyCatalogRepository.save(strategyCatalog);
    }

    @Override
    public List<StrategyCatalog> queryDestId(String destId) {
        return strategyCatalogRepository.findByDestId(destId);
    }

    @Override
    public  List<StrategyCatalog> catalogs(String destId) {
//        List<CatalogVO> list = new ArrayList();
//        CatalogVO vo = new CatalogVO();
//
//        List<StrategyCatalog> strategyCatalogs = this.queryDestId(destId);

//        List<Map<String, Object>> mm = new ArrayList<>();

//        for (StrategyCatalog strategyCatalog : strategyCatalogs) {
//            String name = strategyCatalog.getName();

//            String catalogId = strategyCatalog.getId();

//            vo.setDestName(name);

//            List<Strategy> strategies = strategyService.queryCatalogId(catalogId);

//            for (Strategy strategy : strategies) {
//                    Map map = new HashMap();
//                    map.put("id", strategy.getId());
//                    map.put("title", strategy.getTitle());
//                    mm.add(map);
//            }
 //             vo.setMapList(mm);
//
//            list.add(vo);
//        }
        //攻略分类
        List<StrategyCatalog> list = strategyCatalogRepository.findByDestId(destId);

        //攻略分类下所有的攻略文章, limit 0 3
        for (StrategyCatalog catalog : list) {
            List<Strategy> sts = strategyService.getStrategyByCatalogId(catalog.getId());
            catalog.setStrategies(sts);
        }

        return list;
    }


}
